package com.fusion.lottery.auth.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fusion.lottery.auth.service.UserService;
import com.fusion.lottery.entity.auth.User;
import com.fusion.lottery.mapper.auth.UserMapper;
import com.fusion.utils.json.Msg;
import com.fusion.utils.json.QueryResult;
import com.fusion.utils.string.MD5Util;
import com.fusion.utils.string.StringUtil;
import com.fusion.utils.token.PasswordUtil;

/**
 * 
 * @ClassName: UserServiceImpl 
 * @Description: TODO
 * @author louis
 * @date 2018年3月28日 下午6:19:36
 */
@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserMapper userMapper;
	
	public Msg list(User user,long start,long pageSize) {
		List<User> list = userMapper.list(user,start,pageSize);
		QueryResult qs = new QueryResult(list,userMapper.count(user));
		return new Msg(qs);
	}
	
	public Msg get(String id) {
		User user = userMapper.get(id);
		return new Msg(user);
	}
	
	public Msg add(User user) {
		user.setId(UUID.randomUUID().toString().replaceAll("-", ""));
		user.setCreateTime(System.currentTimeMillis()/1000);
		String salt = PasswordUtil.salt();
		user.setSalt(salt);
		user.setStatus("0");
		user.setType("1");
		user.setPassword(PasswordUtil.hex(user.getPassword(), salt));
		userMapper.insert(user);
		return new Msg(user);
	}
	
	public Msg update(User user) {
		if(StringUtil.isNotNullOrEmpty(user.getPassword())) {
			user.setPassword(MD5Util.hexMD5(user.getPassword()));
		}
		userMapper.update(user);
		return new Msg();
	}
	
	public Msg delete(String id) {
		userMapper.delete(id);
		return new Msg();
	}

}
